package com.trsj.lnsj.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

public class ConfigurableConstants {
  protected static Logger logger = LoggerFactory.getLogger(ConfigurableConstants.class);
  protected static Properties p = new Properties();

  public static void init(String propertyFileName) throws IOException {
    InputStream in = null;
    try {
      logger.debug("Loading the property file [{}]", propertyFileName);
      in = new ClassPathResource(propertyFileName).getURL().openStream();
      if (in != null)
        p.load(in);
    } finally {
      if (in != null)
        in.close();
    }
  }

  public static String getProperty(String key, String defaultValue) {
    return p.getProperty(key, defaultValue);
  }

  public static String getProperty(String key) {
    return p.getProperty(key);
  }

  public static int getIntProperty(String key, int defaultValue) {
    return NumberUtils.toInt(getProperty(key), defaultValue);
  }

  public static int getIntProperty(String key) {
    return getIntProperty(key, 0);
  }
}
